package com.richest.api.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.richest.api.controller.form.MerchantRegisterForm;
import com.richest.api.dao.EmployeeMapper;
import com.richest.api.dao.EmployeeRoleMapper;
import com.richest.api.dao.MerchantAnnouncementMapper;
import com.richest.api.dao.MerchantBaseMapper;
import com.richest.api.dao.MerchantBusinessMapper;
import com.richest.api.dao.MerchantContactMapper;
import com.richest.api.dao.MerchantMapper;
import com.richest.api.dao.MerchantQualificationMapper;
import com.richest.api.service.IMerchantService;
import com.richest.api.utils.MerchantRegisterFormConvertUtil;
import com.richest.bean.EmployeeBean;
import com.richest.bean.EmployeeRoleBean;
import com.richest.bean.MerchantAnnouncementBean;
import com.richest.bean.MerchantBaseBean;
import com.richest.bean.MerchantBean;
import com.richest.bean.MerchantBusinessBean;
import com.richest.bean.MerchantContactBean;
import com.richest.bean.MerchantQualificationBean;

/**
 * 企业信息管理服务实现
 * 
 * @author 杨博
 * @version richest-api 2.0
 */
@Service
public class MerchantServiceImpl implements IMerchantService {

	@Autowired
	private MerchantMapper mapper;

	@Autowired
	private MerchantBaseMapper baseMapper;

	@Autowired
	private MerchantBusinessMapper businessMapper;

	@Autowired
	private MerchantContactMapper contactMapper;

	@Autowired
	private MerchantAnnouncementMapper announcementMapper;

	@Autowired
	private MerchantQualificationMapper qualificationMapper;

	@Autowired
	private EmployeeMapper employeeMapper;

	@Autowired
	private EmployeeRoleMapper employeeRoleMapper;

	@Override
	public MerchantBean merchantSelectOne(Long id) {
		return mapper.merchantSelectOne(id);
	}

	@Override
	public List<Map<String, Object>> merchantSelectMap(Map<String, Object> params) {
		return mapper.merchantSelectMap(params);
	}

	@Override
	public Map<String, Object> merchantRegister(MerchantRegisterForm registerForm) {
		Map<String, Object> map = new HashMap<>();
		MerchantBaseBean baseBean = MerchantRegisterFormConvertUtil.toMerchantBaseBean(registerForm);
		MerchantQualificationBean qualificationBean = MerchantRegisterFormConvertUtil
				.toMerchantQualificationBean(registerForm);
		EmployeeBean employeeBean = MerchantRegisterFormConvertUtil.toEmployeeBean(registerForm);
		baseBean.setCreate_user_id(0L);
		Long merchantId = baseMapper.merchantBaseAdd(baseBean);
		map.put("MerchantId", merchantId);
		employeeBean.setMerchant_id(merchantId);
		Long employeeId = employeeMapper.employeeAdd(employeeBean);
		map.put("EmployeeId", employeeId);
		EmployeeRoleBean employeeRoleBean = new EmployeeRoleBean();
		employeeRoleBean.setEmployee_id(employeeId);
		employeeRoleBean.setRole_id(2L);
		employeeRoleMapper.employeeRoleAdd(employeeRoleBean);
		qualificationBean.setId(merchantId);
		qualificationBean.setCreate_user_id(employeeId);
		qualificationMapper.merchantQualificationAdd(qualificationBean);
		MerchantBusinessBean businessBean = new MerchantBusinessBean();
		businessBean.setId(merchantId);
		businessBean.setCreate_user_id(employeeId);
		businessMapper.merchantBusinessAdd(businessBean);
		MerchantContactBean contactBean = new MerchantContactBean();
		contactBean.setId(merchantId);
		contactBean.setCreate_user_id(employeeId);
		contactMapper.merchantContactAdd(contactBean);
		MerchantAnnouncementBean announcementBean = new MerchantAnnouncementBean();
		announcementBean.setId(merchantId);
		announcementBean.setCreate_user_id(employeeId);
		announcementMapper.merchantAnnouncementAdd(announcementBean);
		return map;
	}

}
